/**
*	插件：通知信息
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.notify = {
	isopen:false,
	init:function(){
		var lx = this.getaccess();
		if(lx=='ok')this.isopen = true;//可以发送
		if(!this.isopen)console.log(''+NOWURL+'的通知的权限没打开');
		//js.notify.send({body:'诶诶过个二二二二二',title:'哈哈'});
	},

	getaccess:function(){
		if(nwjs)return 'ok';
		var lx = 'none';
		if(typeof(Notification)=='undefined'){
			lx='unlock';
			return lx;
		}
		lx = Notification.permission;
		if(lx=='granted'){lx='ok';}else if(lx=='denied'){lx='jz';}else{lx='mr';}
		return lx;
	},
	
	send:function(cans){
		if(!this.isopen)return;
		this.close();
		var can = js.applyIf(cans,{
			body:'',
			icon:'',
			tag:'rockplatnotice',
			title:'提醒',
			click:function(){}
		});
		var clsfun = function(){
			var blx = can.click();
			if(blx){
				if(js.nwjs)js.nwjs.show();
				window.focus();
			}
			js.notify.close();
		}
		if(nwjs){
			localStorage.setItem('xinhuplat_closelx','no');
			var url =NOWURL+'list/reim/notification.html?'+Math.random()+'';
			localStorage.setItem('xinhuplat_notification', JSON.stringify({icon:can.icon,title:can.title,body:can.body}));
			var canss={"frame": false,title:"todo","width": 350,resizable:false,'always_on_top':true,show:false,"height": 110,"show_in_taskbar":false,"transparent":true},me=this;
			nw.Window.open(url,canss,function(wis){
				me.notification = wis;
				wis.on('closed',function(){
					if(js.nwjs)js.nwjs.iconok();
					if(localStorage.getItem('xinhuplat_closelx')=='yes'){
						clsfun();
					}
					me.notification=false;
				});
			});
		}else{
			var notification= new Notification(can.title, can);
			notification.onclick = function(){
				clsfun();
			}
			this.notification = notification;
		}
	},
	close:function(){
		try{
			if(this.notification)this.notification.close(true);
		}catch(e){}
		this.notification = false;
	},
	paly:function(){
		var sid = 'notifys_sound_audio';
		var boa = get(sid);
		var src = 'res/sound/niu.mp3';
		if(!boa){
			var strsr = '<audio id="'+sid+'" src="'+src+'" autoplay="autoplay" hidden="true"></audio>';
			$('body').append(strsr);
		}
		boa = get(sid);
		boa.src=src;
		if(boa.play)boa.play();
	}
}